﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;
using FJASPX.Service;
using System.Collections.Generic;
using FJASPX.Domain;
using FJASPX.Framwork.Helper;
using log4net;

namespace PlugNT.Cms.Web.Admin.Pre_ProductCategory
{
    public partial class Edit : AdminPage
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(Add));
        public IProductCategoryService iProductCategoryService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();
            this.CheckParamte(new object[] { "id" });
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                ProductCategory parent = null;
                ProductCategory productCategory = new ProductCategory();
                if (!"0".Equals(this.DdlMenu.SelectedValue.Trim()))
                {
                    parent = iProductCategoryService.Get(int.Parse(this.DdlMenu.SelectedValue.Trim()));
                } string parentPath = this.DdlMenu.SelectedValue.Trim();
                productCategory.Id = int.Parse(Request.QueryString["id"].ToString());
                productCategory.Name = this.txtClassName.Text.Trim();
                productCategory.ParentId = int.Parse(this.DdlMenu.SelectedValue.Trim());
                productCategory.Sort = int.Parse(this.txtSort.Text.Trim());
                productCategory.TimeStamp = DateTime.Now;
                productCategory.Description = this.txtDescription.Text.Trim();
                productCategory.ParentPath = parent != null ? parent.ParentPath + productCategory.Id.ToString() + "," : parentPath + "," + productCategory.Id + ",";
                iProductCategoryService.Save(productCategory);
                BindData();
                JsHelper.Alert("产品类别保存操作成功！");
                Response.Redirect("Admin.aspx");
            }
            catch (Exception ex)
            {
                JsHelper.Alert("修改产品类别操作失败 ！" + ex.Message);
                logger.Error(ex);
            }
        }

        #region 数据绑定
        private void BindData()
        {

            this.btnEdit.Attributes.Add("onclick", "return ChkInput()");
            IList<ProductCategory> entityList = iProductCategoryService.GetAll("ParentPath", true);
            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("添加为根类别", "0"));
            foreach (ProductCategory entity in entityList)
            {

                int length = entity.ParentPath != null ? entity.ParentPath.Split(',').Length : 0;
                string id = entity.Id.ToString();
                string name = entity.Name.Trim();

                if (length == 0)
                {
                    this.DdlMenu.Items.Add(new ListItem(name, id));

                }
                else
                {
                    name = "├ " + name;
                    name = PlugNT.Common.Utils.StringOfChar(length - 1, "　") + name;
                    this.DdlMenu.Items.Add(new ListItem(name, id));
                }
            }

            this.DdlMenu.Attributes.Add("onchange", "SwapText();");

            ProductCategory productCategory = iProductCategoryService.Get(int.Parse(Request.QueryString["id"]));
            this.txtClassName.Text = productCategory.Name;
            this.hidClassNo.Value = productCategory.Id.ToString();
            this.txtSort.Text = productCategory.Sort.ToString();
            this.txtDescription.Text = productCategory.Description;
            this.DdlMenu.SelectedValue = productCategory.ParentId.ToString();
        }

        #endregion


    }
}
